﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProgNetComponents;

namespace ProgNetDxComponents.Controls.Forms
{
    public partial class InputFormBase : XtraForm
    {
        private BaseEdit _Edit;
        protected bool IsErrorState =false;
        [Category(Categories.ProgNetSettings),
        EditorBrowsable( EditorBrowsableState.Never)]
        public BaseEdit Edit
        {
            get { return _Edit; }
            set
            {
                _Edit = value;
                if (_Edit != null)
                {
                    _Edit.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(_Edit_EditValueChanging);
                }
            }
        }

        void _Edit_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            if (IsErrorState)
            {
                IsErrorState = false;
                dxErrorProvider1.SetError(Edit, "");
            }
        }
        public InputFormBase()
        {
            InitializeComponent();
            if (Application.OpenForms != null && Application.OpenForms.Count > 0)
                this.Icon = Application.OpenForms[0].Icon;

        }
        public bool DoValidateValue
        {
            get;
            set;
        }
        public event EventHandler<ValidationEventArgs> ValidateValue;

        public virtual object GetEditValue()
        {
            if (Edit != null)
                return Edit.EditValue;
            else
                throw new NotImplementedException("Edit is null!");
        }

        public ObjectDialogResult ShowInputDialog(string Caption, string text )
        {
            ObjectDialogResult res = new ObjectDialogResult();
            this.Text = Caption;
            lblText.Text = text;
            res.DialogResult = this.ShowDialog();
            if (res.DialogResult == System.Windows.Forms.DialogResult.OK)
                res.Value = GetEditValue();
            return res;
        }
        public ObjectDialogResult ShowInputDialog(string text = "Value:")
        {
            return ShowInputDialog(Application.ProductName, text);
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        { 
            if (DoValidateValue && ValidateValue != null)
            {
                ValidationEventArgs ea = new ValidationEventArgs(GetEditValue());
                ValidateValue(this, ea);
                if (ea.Error.IsNotNull())
                {
                    IsErrorState = true;
                    e.Cancel = true;
                    dxErrorProvider1.SetError(Edit, ea.Error);
                }
            }
        }
    }
    public class ValidationEventArgs : EventArgs
    {
        private object _Value;
        public ValidationEventArgs(object value)
        {
            _Value = value;
        }
        public object Value
        {
            get { return _Value; }
        }
        public string Error
        {
            get;
            set;
        }
    }
}
